package cn.com.dwr.service.impl;

import cn.com.dwr.common.exception.HubException;
import cn.com.dwr.entity.AppOrder;
import cn.com.dwr.entity.AppOrderItem;
import cn.com.dwr.entity.AppProduct;
import cn.com.dwr.mapper.AppProductMapper;
import cn.com.dwr.service.AppOrderItemService;
import cn.com.dwr.service.AppProductService;
import cn.hutool.core.collection.ListUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import cn.com.dwr.entity.AppSkuStock;
import cn.com.dwr.service.AppSkuStockService;
import cn.com.dwr.mapper.AppSkuStockMapper;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
* @author 邓文润
* @description 针对表【app_sku_stock】的数据库操作Service实现
* @createDate 2023-12-12 07:13:55
*/
@Service
public class AppSkuStockServiceImpl extends ServiceImpl<AppSkuStockMapper, AppSkuStock>
    implements AppSkuStockService{

    @Resource
    AppProductMapper appProductMapper;

    @Resource
    AppSkuStockMapper appSkuStockMapper;

    @Resource
    AppOrderItemService appOrderItemService;

    @Override
    public List<AppSkuStock> listByProductId(Long productId) {

        // 多组合产品
        List<AppSkuStock> skuStocks = this.list(new QueryWrapper<AppSkuStock>()
                .eq("product_id", productId));

        // 单规格产品
        if(skuStocks == null || skuStocks.size() == 0){
            skuStocks = ListUtil.toList(getSingleSkuStokck(productId));
        }
        return skuStocks;
    }

    @Override
    public AppSkuStock getSku(Long skuId, Long productId) {
        if(skuId != -1){
            // 多规格库存
            return this.getById(skuId);
        }
        // 单规格库存
        return getSingleSkuStokck(productId);
    }

    @Override
    public void reduceStock(List<AppOrderItem> orderItems) {

        orderItems.forEach(item -> {
            int count;
            if(item.getSkuId() == -1){
                // 默认规格
                count = appProductMapper.reduceStock(item.getProductId(), item.getQuantity());
            }else{
                // 多规格
                count = appSkuStockMapper.reduceStock(item.getSkuId(), item.getQuantity());
            }
            if(count == 0){
                throw new HubException("库存不足");
            }
        });

    }

    @Override
    public void releaseStock(AppOrder appOrder) {
        List<AppOrderItem> orderItems = appOrderItemService.listByOrderId(appOrder.getId());

        orderItems.forEach(item -> {
            int count;
            if(item.getSkuId() == -1){
                // 默认规格
                count = appProductMapper.releaseStock(item.getProductId(), item.getQuantity());
            }else{
                // 多规格
                count = appSkuStockMapper.releaseStock(item.getSkuId(), item.getQuantity());
            }
            if(count == 0){
                throw new HubException("释放库存失败");
            }
        });
    }

    /**
     * 商品单规格
     * @param productId
     * @return
     */
    private AppSkuStock getSingleSkuStokck(Long productId) {
        AppProduct product = appProductMapper.selectById(productId);

        AppSkuStock skuStock = new AppSkuStock();
        skuStock.setPrice(product.getPrice());
        skuStock.setStock(product.getStock());
        skuStock.setSku("默认规格");
        skuStock.setId(-1L);

        return skuStock;
    }
}




